# load packages

library(here)
library(haven)
library(dplyr)
library(tidyr)
library(ggplot2)
library(ggpubr)
library(patchwork)

# Appendix
## * A.0.1: Bar Graphs of Prior Law Clerkship and other Attorney Characteristics

data <- read.csv("JLC_SSB_Replication_Data_Fig_A01.csv")

for_graph <- data %>%
  dplyr::select(att_name, att_clerkship, att_honors, att_honors, att_osg, att_experienz, att_dcprivate) %>%
  filter(att_name != "", att_name != ".") %>%
  mutate(
    clerk = ifelse(att_clerkship == 6, "scotus", "neither"),
    clerk = ifelse(att_clerkship == 5, "usca", clerk)
  ) %>%
  mutate(
    replayer = ifelse(att_experienz > 0, 1, 0),
    osg = ifelse(att_osg > 0, 1, 0)
  ) %>%
  distinct()

for_graph$clerk <- factor(for_graph$clerk, levels=c("scotus", "usca", "neither"))

clerk_labeller <- function(variable, value) {
  return(c(
    "scotus" = "SCOTUS Clerk",
    "usca" = "USCA Clerk",
    "neither" = "Neither"
  )[value])
}

tiff("graphics/clerk_attributes.tiff", width = 10, height = 6, units = "in", res = 300)

elite_graph <- for_graph %>%
  drop_na(clerk, att_honors) %>%
  ggplot(aes(x = factor(att_honors), fill = factor(att_honors))) +
  geom_bar(aes(y = (after_stat(count))/sum(after_stat(count))*100), position = "dodge") + 
  theme_bw() +
  xlab("") +
  ylab("") +
  ggtitle("Elite Law School") +
  scale_fill_manual(values = c("0" = "grey60", "1" = "grey20"), 
                    labels = c("Not Elite", "Elite")) +
  scale_x_discrete(labels = c("0" = "Not Elite", "1" = "Elite")) +
  labs(fill = "Elite Status") +
  scale_y_continuous(
    breaks = seq(0, 100, by = 10),
    labels = scales::percent_format(scale = 1, accuracy = 1, suffix = "%"),
    expand = expansion(add = c(0.01, 0.01)),
    limits = c(0, 100)
  ) +
  theme(
    legend.position = "none",
    legend.title = element_text(size = 12),
    legend.text = element_text(size = 10),
    axis.text = element_text(size = 7),
    axis.title = element_text(size = 14),
    plot.title = element_text(size = 16, face = "bold")
  ) +
  facet_grid(~ factor(clerk, levels = c("scotus", "usca", "neither")), 
             scales = "free_y",
             labeller = as_labeller(clerk_labeller))

honors_graph <- for_graph %>%
  drop_na(clerk, att_honors) %>%
  ggplot(aes(x = factor(att_honors), fill = factor(att_honors))) +
  geom_bar(aes(y = (after_stat(count))/sum(after_stat(count))*100), position = "dodge") + 
  theme_bw() +
  xlab("") +
  ylab("") +
  ggtitle("Honors/Law Review") +
  scale_fill_manual(values = c("0" = "grey60", "1" = "grey20"), 
                    labels = c("Not Elite", "Elite")) +
  scale_x_discrete(labels = c("0" = "Not Honors/\nLaw Review", "1" = "Honors/\nLaw Review")) +
  labs(fill = "Elite Status") +
  scale_y_continuous(
    breaks = seq(0, 100, by = 10),
    labels = scales::percent_format(scale = 1, accuracy = 1, suffix = "%"),
    expand = expansion(add = c(0.01, 0.01)),
    limits = c(0, 100)
  ) +
  theme(
    legend.position = "none",
    legend.title = element_text(size = 12),
    legend.text = element_text(size = 10),
    axis.text = element_text(size = 7),
    axis.title = element_text(size = 14),
    plot.title = element_text(size = 16, face = "bold")
  ) +
  facet_grid(~ factor(clerk, levels = c("scotus", "usca", "neither")), 
             scales = "free_y",
             labeller = as_labeller(clerk_labeller))

osg_graph <- for_graph %>%
  drop_na(clerk, osg) %>%
  ggplot(aes(x = factor(osg), fill = factor(osg))) +
  geom_bar(aes(y = (after_stat(count))/sum(after_stat(count))*100), position = "dodge") + 
  theme_bw() +
  xlab("") +
  ylab("") +
  ggtitle("OSG") +
  scale_fill_manual(values = c("0" = "grey60", "1" = "grey20"), 
                    labels = c("Not OSG", "OSG")) +
  scale_x_discrete(labels = c("0" = "Not OSG", "1" = "OSG")) +
  labs(fill = "Elite Status") +
  scale_y_continuous(
    breaks = seq(0, 100, by = 10),
    labels = scales::percent_format(scale = 1, accuracy = 1, suffix = "%"),
    expand = expansion(add = c(0.01, 0.01)),
    limits = c(0, 100)
  ) +
  theme(
    legend.position = "none",
    legend.title = element_text(size = 12),
    legend.text = element_text(size = 10),
    axis.text = element_text(size = 7),
    axis.title = element_text(size = 14),
    plot.title = element_text(size = 16, face = "bold")
  ) +
  facet_grid(~ factor(clerk, levels = c("scotus", "usca", "neither")), 
             scales = "free_y",
             labeller = as_labeller(clerk_labeller))

replayer_graph <- for_graph %>%
  drop_na(clerk, replayer) %>%
  ggplot(aes(x = factor(replayer), fill = factor(replayer))) +
  geom_bar(aes(y = (after_stat(count))/sum(after_stat(count))*100), position = "dodge") + 
  theme_bw() +
  xlab("") +
  ylab("") +
  ggtitle("Repeat Player") +
  scale_fill_manual(values = c("0" = "grey60", "1" = "grey20"), 
                    labels = c("Not replayer", "replayer")) +
  scale_x_discrete(labels = c("0" = "Not Repeat\nPlayer", "1" = "Repeat\nPlayer")) +
  labs(fill = "Elite Status") +
  scale_y_continuous(
    breaks = seq(0, 100, by = 10),
    labels = scales::percent_format(scale = 1, accuracy = 1, suffix = "%"),
    expand = expansion(add = c(0.01, 0.01)),
    limits = c(0, 100)
  ) +
  theme(
    legend.position = "none",
    legend.title = element_text(size = 12),
    legend.text = element_text(size = 10),
    axis.text = element_text(size = 7),
    axis.title = element_text(size = 14),
    plot.title = element_text(size = 16, face = "bold")
  ) +
  facet_grid(~ factor(clerk, levels = c("scotus", "usca", "neither")), 
             scales = "free_y",
             labeller = as_labeller(clerk_labeller))

dcprivate_graph <- for_graph %>%
  drop_na(clerk, att_dcprivate) %>%
  ggplot(aes(x = factor(att_dcprivate), fill = factor(att_dcprivate))) +
  geom_bar(aes(y = (after_stat(count))/sum(after_stat(count))*100), position = "dodge") + 
  theme_bw() +
  xlab("") +
  ylab("") +
  ggtitle("D.C. Private") +
  scale_fill_manual(values = c("0" = "grey60", "1" = "grey20"), 
                    labels = c("Not att_dcprivate", "att_dcprivate")) +
  scale_x_discrete(labels = c("0" = "Not D.C.\nPrivate", "1" = "D.C. Private")) +
  labs(fill = "Elite Status") +
  scale_y_continuous(
    breaks = seq(0, 100, by = 10),
    labels = scales::percent_format(scale = 1, accuracy = 1, suffix = "%"),
    expand = expansion(add = c(0.01, 0.01)),
    limits = c(0, 100)
  ) +
  theme(
    legend.position = "none",
    legend.title = element_text(size = 12),
    legend.text = element_text(size = 10),
    axis.text = element_text(size = 7),
    axis.title = element_text(size = 14),
    plot.title = element_text(size = 16, face = "bold")
  ) +
  facet_grid(~ factor(clerk, levels = c("scotus", "usca", "neither")), 
             scales = "free_y",
             labeller = as_labeller(clerk_labeller))



((elite_graph / honors_graph) | (osg_graph / replayer_graph / dcprivate_graph)) + plot_layout(guides = 'collect')


dev.off()